package qmm.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import qmm.dao.FundDao;
import qmm.dao.impl.FundDaoImpl;
import qmm.entity.Fund;
import qmm.entity.User;

@WebServlet("/FundServlet")
public class FundServlet extends BaseServlet{
	
	FundDao dao = new FundDaoImpl();
	
	public void findByUserId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		List<Fund> fundList = dao.findByUserId(user.getId());
		System.out.println(fundList);
		double balance = fundList.get(0).getBalance();
		session.setAttribute("fundList", fundList);
		session.setAttribute("balance", balance);
		response.sendRedirect("fund.jsp");
	}
	
	public void charge(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		double money = Double.parseDouble(request.getParameter("money"));
		String vipRemark = request.getParameter("vipRemark");
		double balance = (double) session.getAttribute("balance");
		
		int n = dao.charge(user.getId(), money, balance, vipRemark);
		
		findByUserId(request,response);
	}

}
